<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 个人信息
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisBill;
use app\ais\model\AisBank;
use app\ais\model\AisBankCash;
use app\ais\model\AisCouponUser;
use app\ais\model\AisConfig;
use app\ais\model\AisVip;
use app\ais\model\AisVipUser;
use app\ais\model\AisStore;
use app\common\model\SystemUserLevel;
use think\helper\Time;

class User extends Base{
    
    /**
     * 个人信息
     * @return void
     */
    public function initialize() {
        parent::initialize();
        $this->isUserAuth();
    }

   /**
     * 判断是否会员,并返回会员数据
     */
    public function vip(){
        $this->apiSign();
        $data['is_vip'] = 0;
        $data['info']   = [];
        $rel = AisVipUser::where(['is_lock' => 0,'uid' => $this->user->id])->find();
        if(!empty($rel)){
            $data['is_vip'] = 1;
            $data['info']  = AisVip::where(['is_lock' => 0,'id' => $rel->vip_id])->find();
        }
        return enjson(200,$data);
    }

 
   /**
     * 帐号数据分析
     * icp_award:服务商月业绩提成
     */
    public function analysis(){
        $this->apiSign();
        $data = [
            'bank' => ['money'=> 0,'tobo_paid' => 0,'point'=> 0,'coupon_save' => 0,'icp_award' => 0],
            'num'  => ['fans'=> 0,'store' => 0,'vip'=> 0]
        ];
        $bank = AisBank::where(['uid'=>$this->user->id])->field('money,tobo_paid,point')->find();
        if(!empty($bank)){
            $data['bank'] = $bank;
            $data['bank']['icp_award'] = 0; //计算月返利多少钱
        }
        $uids = SystemUserLevel::where(['parent_id' => $this->user->id,'level' => 1])->column('user_id');
        $data['num']['fans']         = count($uids);  //粉丝数量
        $data['num']['vip']          = AisVipUser::where(['is_lock' => 0])->whereIn('uid',$uids)->count();  //会员数量
        $data['num']['store']        = AisStore::where(['is_lock' => 0])->whereIn('manage_uid',$uids)->count(); //邀请开通的好店数量
        $data['bank']['coupon_save'] = AisCouponUser::where(['types' => 3,'is_end' => 0])->count();  //储值券
        return enjson(200,$data);
    }


   /**
     * 查看帐号余额
     */
    public function balance(){
        $this->apiSign();
        $info = AisBank::where(['uid'=>$this->user->id])->field('money,tobo_paid,point')->find()->toArray();
        if(empty($info)){
            $info = ['money' => 0,'tobo_paid' => 0,'point'=> 0];
        }
        return enjson(200,$info);
    }

    /**
     * 提现申请
     **/ 
    public function cash(){
        if (request()->isPost()) {
            $param = [
                'money'         => $this->request->param('money/f'),
                'safepassword'  => $this->request->param('safepassword/s'),
            ];
            $this->apiSign($param);
            $validate = $this->validate($param,'Bank.cash_out');
            if (true !== $validate) {
                return json(['code'=>403,'msg'=>$validate]);
            }
            //判断安全密码是否正确
            if(!password_verify(md5($param['safepassword']),$this->user->safe_password)) {
                return json(['code'=>403,'msg'=>'安全密码不正确']);
            }
            //判断提现周期
            $is_cash = AisBankCash::where(['uid' => $this->user->id,'state' => 0])->count();
            if($is_cash){
                return json(['code'=>403,'msg'=>'还有提现再审核中,禁止连续提现']);
            }
            //减少
            $rel = AisBank::cashOut($this->user->id,$param['money']);
            if(empty($rel)){
                return json(['code'=>403,'msg'=>"余额不足"]);
            }
            return enjson(200,"提现成功");
        }
    }

   /**
     * 账单
     */
    public function bill(){
        $param['type'] = $this->request->param('type/d',0);
        $param['page'] = $this->request->param('page/d',1);
        $param['sign'] = $this->request->param('sign');
        $this->apiSign($param);
        $condition[] = ['uid','=',$this->user->id];
        if($param['type']){
            $condition[] = ['money','<',0]; //历史
        }else{  
            $condition[] = ['money','>',0]; //今天
        }
        $info = AisBill::withAttr('update_time', function ($value, $data) {
            return date('Y-m-d H:i',$value);
        })->with(['user'=> function($query) {
            $query->field('id,face,nickname');
        }])->where($condition)->order('id desc')->page($param['page'],10)->select();
        if($info->isEmpty()){
            return enjson(204);
        }
        return enjson(200,$info);
    }
}